package Servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Martin
 */
//@WebServlet(name="ValidarSesion", urlPatterns={"Servlets/ValidarSesion"})
public class ValidarSesion extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    Statement st;
    ResultSet rs;
    HttpSession sesion;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            String user=request.getParameter("usuario");
            String contra=request.getParameter("pass");

            //Creamos conexion a la BD
            Class.forName("com.mysql.jdbc.Driver");
            Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/tpare", "root", "root");
            
            //Creamos el statement para traer los datos de la BD
            st = conexion.createStatement();

            //El resultSet nos guarda lo que trae el statement
            rs= st.executeQuery("Select nombreUsuario, password from usuario where nombreUsuario='"+user+"'");

            rs.next();

            //Comparamos la contraseña que ingreso, con la que trajimos de la BD
            if(rs.getString("password").compareTo(contra)==0)
                //Si todo anda bien, cargamos los datos de la sesion y vamos a LoginOk
            {
                sesion = request.getSession(true);
                sesion.setAttribute("USER", user);
              //  response.sendRedirect("LoginOk.jsp");
                request.getRequestDispatcher("Inicio.jsp").forward(request, response);
            }
            else
                //Si puso algo mal, vamos de nuevo al index
            {
               // response.sendRedirect("index.jsp");
                request.getRequestDispatcher("Inicio.jsp").forward(request, response);
            }

        } catch(Exception e) {}


        finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
